package com.xy.manage.action;



import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.octo.captcha.service.CaptchaServiceException;
import com.octo.captcha.service.image.ImageCaptchaService;
import com.xy.common.page.Pagination;
import com.xy.common.util.DictUtils;
import com.xy.common.security.BadCredentialsException;
import com.xy.common.security.UsernameNotFoundException;
import com.xy.common.web.CookieUtils;
import com.xy.common.web.RequestUtils;
import com.xy.core.web.WebErrors;

import com.xy.manage.model.Treply;
import com.xy.manage.service.TreplyService;



@Controller
public class TreplyAction{
	private static final Logger log = LoggerFactory.getLogger(TreplyAction.class);
	public TreplyAction() {
	}
 
 
 	/**
	 * 民意警务论坛回复表新增页面
	 * 
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_treplyAdd.do")
	public String add(Integer tid,HttpServletRequest request, ModelMap model) {
		model.addAttribute("treply", new Treply());
		model.addAttribute("tid", tid);
        setDict(model);
		return "treplyEdit";
	}

	/**
	 * 民意警务论坛回复表新增保存
	 * 
	 * @param treply
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/o_treplySave.do")
	public String save(Treply treply, HttpServletRequest request, ModelMap model) {
		WebErrors errors = validateSave(treply, request);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		treply = treplyService.save(treply);
		if(null!=treply.getTtopic()&&null!=treply.getTtopic().getTid())
			return "redirect:v_ttopicDetail.do?tid="+treply.getTtopic().getTid();
		else
			return "redirect:v_ttopicList.do";
	}

	/**
	 * 民意警务论坛回复表删除
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping("o_treplyDelete.do")
	public String delete(long[] ids) {
		Long tid = null;
		if (ids != null) {
			Treply t  = treplyService.get(ids[0]);
			tid = t.getTtopic().getTid();
			for (long id : ids) {
				treplyService.remove(id);
			}
		}
		return "redirect:v_ttopicDetail.do?tid="+tid;
	}

	/**
	 * 民意警务论坛回复表编辑页面
	 * 
	 * @param rid
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_treplyEdit.do")
	public String edit(long rid, HttpServletRequest request, ModelMap model) {
		Treply treply = treplyService.get(rid);
		model.addAttribute("treply", treply);
      model.addAttribute("actionEdit", request.getParameter("actionEdit"));
        setDict(model);
		return "treplyEdit";
	}

	/**
	 * 民意警务论坛回复表详情
	 * 
	 * @param rid
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_treplyDetail.do")
	public String detail(long rid, HttpServletRequest request, ModelMap model) {
		Treply treply = treplyService.get(rid);
		model.addAttribute("treply", treply);
        setDict(model);
		return "treplyView";
	}

	/**
	 * 民意警务论坛回复表查询（分页）
	 * 
	 * @param treply
	 * @param pageNo
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_treplyList.do")
	public String listPage(Treply treply, Integer pageNo,
			HttpServletRequest request, ModelMap model) {
		if (pageNo == null)
			pageNo = 1;
		Pagination pagination = treplyService.getTreplyByPage(toMap(treply),
				pageNo, CookieUtils.getPageSize(request));
		model.addAttribute("pagination", pagination);
        setDict(model);
		return "treplyList";
	}

	/**
	 * 民意警务论坛回复表查询
	 * 
	 * @param treply
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_treplyList1.do")
	public String list(Treply treply, HttpServletRequest request, ModelMap model) {
		List<Treply> list = treplyService.getTreply(toMap(treply));
		model.addAttribute("list", list);
        setDict(model);
		return "treplyList";
	}


 	/***
	 * 将页面输入信息转成map
	 * 
	 * @param treply
	 * @return
	 */	private Map<String,Object> toMap(Treply treply){
		Map<String,Object> map=new HashMap<String, Object>();
      		if(treply.getRid()!=null&&!treply.getRid().equals("")){
			map.put("Rid", treply.getRid());
		}
		if(treply.getTtopic()!=null&&!treply.getTtopic().equals("")){
			map.put("Tid", treply.getTtopic());
		}
		if(treply.getContent()!=null&&!treply.getContent().equals("")){
			map.put("Content", treply.getContent());
		}
		if(treply.getCreatetime()!=null&&!treply.getCreatetime().equals("")){
			map.put("Createtime", treply.getCreatetime());
		}  
		if(treply.getUsername()!=null&&!treply.getUsername().equals("")){
			map.put("Username", treply.getUsername());
		}
		return map;
	}
   
	/**
	 * 设置页面所需的数据字典
	 * @param model
	 */
	private void setDict(ModelMap model){
		
	}

	/**
	 * 数据校验
	 * @param model
	 */
	private WebErrors validateSave(Treply treply, HttpServletRequest request) {
		WebErrors errors = WebErrors.create(request);
		/**if (errors.ifOutOfLength(username, "username", 1, 100)) {
			return errors;
		}  */    
		return errors;
	}

   
      
	@Resource
	private TreplyService treplyService;   
}